import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import javax.swing.*;
import java.io.IOException;
import java.time.LocalTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class WeatherPrinter implements Runnable{
    JLabel label = null;
    public WeatherPrinter(JLabel label){
        this.label=label;
    }
    @Override
    public void run() {
        while (true) {
            String urlStr = "https://tianqi.moji.com/weather/china/chongqing/shapingba-district"; // 网址
            Document doc = null;
            try {
                doc = Jsoup.connect(urlStr).get();
                List<String> forecast= doc.getElementsByClass("days clearfix").eachText();
                String [] messages=forecast.get(1).split(" ");
                String kind=messages[1];
                String temperature=messages[2]+messages[3]+messages[4];
                String res = "沙坪坝区 "+kind+" "+temperature;
                label.setText(res);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                //十分钟更新一次天气预报信息
                Thread.sleep(10*60*1000);
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }

        }

    }


}
